package controle;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import modelo.Turma;

/**
 * Servlet implementation class CadastrarAluno
 */
@WebServlet("/cadastrarTurma")
public class CadastrarTurma extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CadastrarTurma() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Turma turma;
		DateFormat format = new SimpleDateFormat("dd/MM/yyyy");  
		
		try{
			Integer cod = Integer.valueOf(request.getParameter("cod"));
			String nome  = request.getParameter("nome");
			Date dtEntrada = (Date) format.parse(request.getParameter("dtEntrada"));
			turma = new Turma(cod, nome, dtEntrada);
			System.out.println("Nome: "+turma.getNome()+" Codigo: "+turma.getCod()+" DtEntrada "+turma.getDtEntrada());
		}catch(Exception e){
			String erro = "O código :"+request.getParameter("cod")
					+"ou o nome: "+request.getParameter("nome")
					+"ou a data de entrada: "+request.getParameter("dtEntrada")
					+"Não são entradas válidas";
			request.setAttribute("erro", erro);
			request.setAttribute("url", "cadastroTurma.jsp");
			request.getRequestDispatcher("/erro").forward(request, response);
		}
		
		
		
			
		
	}

}
